/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Order.Entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

/**
 *
 * @author Administrator
 */
@Entity
@Table(name = "orders")
public class Order implements Serializable {
    private Integer id;    
    private String createDate;
    private String status;
    private Integer total;
    private List<OrderLine> orderlines;
    private Customer customer;
    private String action;

    public Order() {
    }

    public Order(String create_date, Customer customer,
                  String status, Integer total,String action) {
        this.customer = customer;
        this.createDate = create_date;
        this.customer = customer;
        this.status = status;
        this.total = total;
        this.action = action;
    }

    @Transient
    public String getAction() {
        return action;
    }
    public void setAction(String action) {
        this.action = action;
    }

    @Column(name="create_date")
    public String getCreateDate() {
        return createDate;
    }    
    public void setCreateDate(String create_date) {
        this.createDate = create_date;
    }

    @Id
    @Column(name = "id")
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    @Column(name = "status")
    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    @Column(name="total")
    public Integer getTotal() {
        return total;
    }

    public void setTotal(Integer ord_total) {
        this.total = ord_total;
    }

    @OneToMany(mappedBy = "order", cascade = {CascadeType.PERSIST, CascadeType.REMOVE, CascadeType.MERGE}, fetch = FetchType.EAGER)
    public List<OrderLine> getOrderlines() {
        if(orderlines == null)
        {
            orderlines = new ArrayList<OrderLine>();
        }
        return orderlines;
    }

    public void setOrderlines(List<OrderLine> orderlines) {
        this.orderlines = orderlines;
    }

    public OrderLine addOrderline(OrderLine orderline) {
        getOrderlines().add(orderline);
       // orderline.setOrder(this);
        return orderline;
    }

    public OrderLine removeOrderline(OrderLine orderline) {
        getOrderlines().remove(orderline);
        orderline.setOrder(null);
        return orderline;
    }

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "customer_id", insertable = true, updatable = true)
    public Customer getCustomer() {
        return customer;
    }

    public void setCustomer(Customer customer) {
        this.customer = customer;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        if (!(object instanceof Order)) {
            return false;
        }
        Order other = (Order) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "Order.Entity.Order[id=" + id + "]";
    }
}
